package com.cindata

import grails.transaction.Transactional
import net.sf.json.JSON
import org.springframework.security.access.annotation.Secured

@Secured(['isAuthenticated()'])
@Transactional
class RiskWarningConfigureController
{
    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def componentService

    def index()
    {
        def result = RiskWarningConfigure.list().get(0)
        respond result, model: [result: result, componentService: componentService]
    }

    // 押品价值波动风险配置
    @Transactional
    def modifyValue()
    {
        Thread.sleep(1500)
        def id = params['ltid']
        def valueRiseWeight = params['valueRiseWeight']
        def valueDeclineWeight = params['valueDeclineWeight']
        def riskWarningConfigure = RiskWarningConfigure.findById(id)

        if (!riskWarningConfigure)
        {
            render(["failed"] as JSON)
            return
        }

        riskWarningConfigure.valueRiseWeight = new BigDecimal(valueRiseWeight).divide(new BigDecimal("100"))
        riskWarningConfigure.valueDeclineWeight = new BigDecimal(valueDeclineWeight).divide(new BigDecimal("100"))
        riskWarningConfigure.save(flush: true)
        render(["success"] as JSON)
    }

    // 逾期/违约（对公/个人）配置
    @Transactional
    def modifyOverDue()
    {
        Thread.sleep(1500)
        def id = params['ltid']
        def contactOverdueWeight = params['contactOverdueWeight']
        def contactBreachWeight = params['contactBreachWeight']
        def accountOverdueWeight = params['accountOverdueWeight']
        def accountBreachWeight = params['accountBreachWeight']
        def riskWarningConfigure = RiskWarningConfigure.findById(id)

        if (!riskWarningConfigure)
        {
            render(["failed"] as JSON)
            return
        }

        riskWarningConfigure.contactOverdueWeight = new BigDecimal(contactOverdueWeight)
        riskWarningConfigure.contactBreachWeight = new BigDecimal(contactBreachWeight)
        riskWarningConfigure.accountOverdueWeight = new BigDecimal(accountOverdueWeight)
        riskWarningConfigure.accountBreachWeight = new BigDecimal(accountBreachWeight)
        riskWarningConfigure.save(flush: true)
        render(["success"] as JSON)
    }

    // 集中度风险综
    @Transactional
    def modifyConcentrationRisk()
    {
        Thread.sleep(1500)
        def id = params['ltid']
        def concentrationRiskWeight = params['concentrationRiskWeight']
        def rwc = RiskWarningConfigure.findById(id)
        if (!rwc)
        {
            render(["failed"] as JSON)
            return
        }

        rwc.concentrationRiskWeight = new BigDecimal(concentrationRiskWeight)
        rwc.save(flush: true)
        render(["success"] as JSON)
    }

    // 覆盖率
    @Transactional
    def modifyCoverRate()
    {
        Thread.sleep(1500)
        def id = params['ltid']
        def coverRateOfCityWeight = params['coverRateOfCityWeight']
        def coverRateOfCommunityWeight = params['coverRateOfCommunityWeight']
        def rwc = RiskWarningConfigure.findById(id)
        if (!rwc)
        {
            render(["failed"] as JSON)
            return
        }

        rwc.coverRateOfCityWeight = new BigDecimal(coverRateOfCityWeight).divide(new BigDecimal("100"))
        rwc.coverRateOfCommunityWeight = new BigDecimal(coverRateOfCommunityWeight).divide(new BigDecimal("100"))
        rwc.save(flush: true)
        render(["success"] as JSON)
    }

    // 舆情
    @Transactional
    def modifyPublicOpinion()
    {
        Thread.sleep(1500)
        def id = params['ltid']
        def publicOpinionsWeight = params['publicOpinionsWeight']
        def rwc = RiskWarningConfigure.findById(id)
        if (!rwc)
        {
            render(["failed"] as JSON)
            return
        }

        rwc.publicOpinionsWeight = publicOpinionsWeight
        rwc.save(flush: true)
        render(["success"] as JSON)
    }

    // 有效期
    @Transactional
    def modifyEffectiveDays()
    {
        Thread.sleep(1500)
        def id = params['ltid']
        def effectiveDaysWeight = params['effectiveDaysWeight']
        def rwc = RiskWarningConfigure.findById(id)
        if (!rwc)
        {
            render(["failed"] as JSON)
            return
        }

        rwc.effectiveDaysWeight = new BigDecimal(effectiveDaysWeight)
        rwc.save(flush: true)
        render(["success"] as JSON)
    }

    // 楼盘评分
    @Transactional
    def modifyBuildingRated()
    {
        Thread.sleep(1500)
        def id = params['ltid']
        def buildingRatedWeight = params['buildingRatedWeight']
        def rwc = RiskWarningConfigure.findById(id)
        if (!rwc)
        {
            render(["failed"] as JSON)
            return
        }

        rwc.buildingRatedWeight = new BigDecimal(buildingRatedWeight)
        rwc.save(flush: true)
        render(["success"] as JSON)
    }

    // 借款人预警
    @Transactional
    def modifyContactRisk()
    {
        Thread.sleep(1500)
        def id = params['ltid']
        def contactCreditWeight = params['contactCreditWeight']
        def rwc = RiskWarningConfigure.findById(id)
        if (!rwc)
        {
            render(["failed"] as JSON)
            return
        }

        rwc.contactCreditWeight = contactCreditWeight
        rwc.save(flush: true)
        render(["success"] as JSON)
    }
}
